﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VMWareLabWorkflowsDataContracts.VMWareLabDetailsDialogControls;

namespace VMWareLabWorkflowsDataContracts
{
    using System.Windows;

    using VMWareLabWorkflowsDataContracts.Properties;

    using MessageBox = System.Windows.Forms.MessageBox;

    public partial class VMWareLabDetailsDialog : Form
    {
        public VMWareLabWorkflowDetails VMWareLabWorkflowDetails { get; set; }
        public WelcomeControl WelcomeControl { get; set; }
        public EnvironmentDetailsControl EnvironmentDetailsControl { get; set; }
        public BuildDetailsControl BuildDetailsControl { get; set; }
        public DeploymentDetailsControl DeploymentDetailsControl { get; set; }
        public TestDetailsControl TestDetailsControl { get; set; }
        private List<string> controlNames;
        private int currentControlNameIndex;

        public VMWareLabDetailsDialog()
        {
            InitializeComponent();

            this.WelcomeControl = new WelcomeControl();
            this.EnvironmentDetailsControl = new EnvironmentDetailsControl();
            this.BuildDetailsControl = new BuildDetailsControl();
            this.DeploymentDetailsControl = new DeploymentDetailsControl();
            this.TestDetailsControl = new TestDetailsControl();
            this.navigationControl1.OnSelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(navigationControl1_OnSelectionChanged);
            this.controlNames = new List<string>() { "Welcome", "Environment", "Build", "Deploy", "Test" };
            this.currentControlNameIndex = 0;
            VMWareWizardData.VMWareWizzardDataInstance.CurrentControlName = "Welcome";
            this.SetButtonState();
        }

        /// <summary>
        /// Handles the OnSelectionChanged event of the navigationControl1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void navigationControl1_OnSelectionChanged(object sender, EventArgs e)
        {
            this.SwitchControl(((System.Windows.Controls.RadioButton)sender).Name);
        }

        /// <summary>
        /// Switches the control.
        /// </summary>
        /// <param name="controlName">Name of the control.</param>
        private void SwitchControl(string controlName)
        {
            try
            {
                if (VMWareWizardData.VMWareWizzardDataInstance.CurrentControlName != controlName)
                {
                    switch (controlName)
                    {
                        case "Welcome":
                            ContentElementHost.Child = this.WelcomeControl;
                            this.lblTitle.Text = Resources.WelcomeTitle;
                            this.currentControlNameIndex = 0;
                            VMWareWizardData.VMWareWizzardDataInstance.CurrentControlName = "Welcome";
                            break;
                        case "Environment":
                            ContentElementHost.Child = this.EnvironmentDetailsControl;
                            this.lblTitle.Text = Resources.EnvironmentTitle;
                            this.currentControlNameIndex = 1;
                            VMWareWizardData.VMWareWizzardDataInstance.CurrentControlName = "Environment";
                            break;
                        case "Build":
                            ContentElementHost.Child = this.BuildDetailsControl;
                            this.lblTitle.Text = Resources.BuildTitle;
                            this.currentControlNameIndex = 2;
                            VMWareWizardData.VMWareWizzardDataInstance.CurrentControlName = "Build";
                            break;
                        case "Deploy":
                            ContentElementHost.Child = this.DeploymentDetailsControl;
                            this.lblTitle.Text = Resources.DeploymentTitle;
                            this.currentControlNameIndex = 3;
                            VMWareWizardData.VMWareWizzardDataInstance.CurrentControlName = "Deploy";
                            break;
                        case "Test":
                            ContentElementHost.Child = this.TestDetailsControl;
                            this.lblTitle.Text = Resources.TestTitle;
                            this.currentControlNameIndex = 4;
                            VMWareWizardData.VMWareWizzardDataInstance.CurrentControlName = "Test";
                            break;
                    }
                }
                this.SetButtonState();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error switching control", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        /// <summary>
        /// Handles the Click event of the btnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the btnPrevious control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (this.currentControlNameIndex > 0)
            {
                this.currentControlNameIndex--;
                this.SwitchControl(this.controlNames[this.currentControlNameIndex]);
            }
            this.SetButtonState();
        }

        /// <summary>
        /// Handles the Click event of the btnNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (this.currentControlNameIndex < this.controlNames.Count - 1)
            {
                this.currentControlNameIndex++;
                this.SwitchControl(this.controlNames[this.currentControlNameIndex]);
            }
            this.SetButtonState();
        }

        /// <summary>
        /// Sets the state of the button.
        /// </summary>
        private void SetButtonState()
        {
            if (this.currentControlNameIndex == 0)
            {
                this.btnPrevious.Enabled = false;
                this.btnNext.Enabled = true;
            }
            else if (this.currentControlNameIndex > 0 && this.currentControlNameIndex < this.controlNames.Count - 1)
            {
                this.btnPrevious.Enabled = true;
                this.btnNext.Enabled = true;
            }
            else if (this.currentControlNameIndex == this.controlNames.Count - 1)
            {
                this.btnPrevious.Enabled = true;
                this.btnNext.Enabled = false;
            }
        }
    }
}
